<?php

namespace App\Http\Admin\Services;

class BaseService
{
    protected $model;
    public function __construct($model = '')
    {
        $this->model = $model;
    }

    //密码加密规则
    public function generatePassword($password = '',$salt = ''){
        $str = $password.$salt;
        return md5($str);
    }

    //批量删除
    public function delModel($ids){
        if(!is_array($ids)){
            throw new InvalidRequestException('请传数组');
        }
        if(count($ids) == 0){
            throw new InvalidRequestException('缺少id');
        }
        $result = $this->model->whereIn('id',$ids)->delete();
        return $result;
    }

    //获取用户所有权限名
    public function getUserPermissionNames(){
        $names = $_SERVER['current_user']->permissions->pluck('name')->toArray();
        return $names;
    }

    //辩证当前用户有没有某个权限
    public function hasPermission($permission_name){
        $flag = false;
        $all_names = $this->getUserPermissionNames();
        if(in_array($permission_name,$all_names)){
            $flag = true;
        }
        return $flag;
    }

    //获取当前用户id
    public function getCurrentUserId(){
        return  $_SERVER['current_user']['id'];
    }


}
